package com.sgb.kjwl.view.ui.fragment.self;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentDeviceStateBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.user.DeviceStateEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.DevicePublishEvent;
import com.sgb.kjwl.view.ui.activity.self.publish.DeviceListActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.DevicePublishActivity;
import com.sgb.kjwl.view.ui.adapter.self.publish.DeviceStateAdapter;
import com.sgb.kjwl.viewmodel.self.publish.DeviceStateModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author zhengkewen
 */
public class DeviceStateFragment extends AppFragment {
    public static final String TAG = "";
    public static final String KEY_TYPE = "type";

    private FragmentDeviceStateBinding binding;
    private String type = "";
    private DeviceStateAdapter deviceStateAdapter;
    @Inject
    DeviceStateModel viewModel;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static DeviceStateFragment newInstance(String type) {
        DeviceStateFragment fragment = new DeviceStateFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_device_state, container, false);
        binding.setViewModel(viewModel);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        type = (String) getArguments().get(KEY_TYPE);
        initView();
        initData();
    }

    private void initView() {
        if (deviceStateAdapter == null) {
            deviceStateAdapter = new DeviceStateAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvView.setLayoutManager(layoutManager);
        binding.rlvView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getActivity().getResources().getDimension(R.dimen.dp10), getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.rlvView.setAdapter(deviceStateAdapter);
        binding.rlvView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getDeviceStateModel(type,((DeviceListActivity)getActivity()).getKeyWord());
            }

            @Override
            public void onLoadMore() {
                viewModel.getDeviceStateMoreModel(type,((DeviceListActivity)getActivity()).getKeyWord());
            }
        });
        deviceStateAdapter.setOnItemClick(new AdapterOnItemClick<DeviceStateEntity>() {
            @Override
            public void onItemClick(DeviceStateEntity deviceStateEntity, int position) {
//                DevicePublishDetailActivity.start(getActivity(), deviceStateEntity.getId());

//                EventBus.getDefault().postSticky(DevicePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_DEVICE_EDIT).setMessageData(deviceStateEntity.getId()));
//                DevicePublishActivity.start(getActivity());
                viewModel.getH5Url(deviceStateEntity.getId()+"&iosFlag=2");
//                DeviceDetailActivity.start(getActivity(), deviceStateEntity.getId(),type);
            }
        });
        deviceStateAdapter.setOptCallBack(new DeviceStateAdapter.OptCallBack() {
            @Override
            public void del(String id) {
                viewModel.delModel(id);
            }

            @Override
            public void onlinOff(String id, String state) {
                viewModel.onlinOffModel(id, state);
            }

            @Override
            public void edit(DeviceStateEntity entity) {
//                EventBus.getDefault().postSticky(DevicePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_DEVICE_EDIT).setMessageData(entity.getId()));
                DevicePublishActivity.start(getActivity(),entity.getId());

            }
        });
    }

    private void initData() {
        viewModel.getDeviceStateModel(type,((DeviceListActivity)getActivity()).getKeyWord());
    }

    public void onlineOffSuccess(String state) {
        if (state.equals("1")) {
            MToast.showToast(getActivity(), "上架成功!");
        } else if (state.equals("2")) {
            MToast.showToast(getActivity(), "下架成功!");
        }
        EventBus.getDefault().post(DevicePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_DEVICE_REFUSH));
    }

    public void delSuccess() {
        MToast.showToast(getActivity(), "删除成功!");
        EventBus.getDefault().post(DevicePublishEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_DEVICE_REFUSH));
    }

    public void refresh(List<DeviceStateEntity> datas) {
        binding.emptyView.setVisibility(View.GONE);
        binding.rlvView.setVisibility(View.VISIBLE);
        binding.rlvView.setLoadingMoreEnabled(true);
        deviceStateAdapter.setDatas(datas);
        binding.rlvView.refreshComplete();
        if (deviceStateAdapter.getItemCount() >= viewModel.total) {
            binding.rlvView.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.rlvView.refreshComplete();
//        binding.rlvView.setVisibility(View.GONE);
        deviceStateAdapter.setDatas(new ArrayList<>());
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    public void loadMore(List<DeviceStateEntity> datas) {
        deviceStateAdapter.addDatas(datas);
        binding.rlvView.loadMoreComplete();
        if (deviceStateAdapter.getItemCount() >= viewModel.total) {
            binding.rlvView.setLoadingMoreEnabled(false);
        }
    }

    public void loadMoreComplete() {
        binding.rlvView.loadMoreComplete();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(DevicePublishEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PUBLISH_DEVICE_REFUSH) {
            viewModel.getDeviceStateModel(type,((DeviceListActivity)getActivity()).getKeyWord());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
